/**
 *    Copyright 2010-2016 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.springboot.jpetstore.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.jpetstore.domain.Account;
import org.springboot.jpetstore.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Eduardo Macarron
 *
 */
@Service
public class AccountServiceBean {

  private static final Logger logger = LoggerFactory.getLogger(AccountServiceBean.class);
  @Autowired
  private AccountMapper accountMapper;

  /**
   *
   * <save one>
   * @param account
   * @return Long
   */
  public void insert(Account account){
    if(logger.isDebugEnabled()){
      logger.debug("AccountServiceBean.insert(Account account) start-->", account);
    }

    int i=accountMapper.insert(account);
    if(logger.isDebugEnabled()){
      logger.debug("AccountServiceBean.insert(Account account) end-->", i);
    }
  }
  /**
   *
   * <update one>
   */
  public int update(Account account){
    if(logger.isDebugEnabled()){
      logger.debug("AccountServiceBean.update(Account account) start-->", account);
    }


    int u=accountMapper.update(account);
    if(logger.isDebugEnabled()){
      logger.debug("AccountServiceBean.update(Account account) end-->", u);
    }
    return u;
  }
  /**
   *
   * <find one by id>
   * @param id
   * @return Account
   */
  public Account get(Long id){
    if(logger.isDebugEnabled()){
      logger.debug("AccountServiceBean.get(Long id) start-->", id);
    }

    Account account=accountMapper.get(id);
    if(logger.isDebugEnabled()){
      logger.debug("AccountServiceBean.get(Long id) end-->", account);
    }
    return account;
  }
  /**
   * <query all>
   * @param paramMap
   * //@param pageBounds
   * @return List<Account>
   */
  public List<Account> search(Map paramMap ){
    if(logger.isDebugEnabled()){
      logger.debug("AccountServiceBean.search(Map paramMap ,PageBounds pageBounds) start-->", paramMap);
    }
    if(ObjectUtils.isEmpty(paramMap)){

    }

    List<Account> accountList=accountMapper.search(paramMap);
    if(logger.isDebugEnabled()){
      logger.debug("AccountServiceBean.search(Map paramMap ,PageBounds pageBounds) end-->", accountList);
    }
    return accountList;
  }


  public Account getAccount(String username) {
    return accountMapper.getAccountByUsername(username);
  }

  public Account getAccount(String username, String password) {
    return accountMapper.getAccountByUsernameAndPassword(username, password);
  }

  @Transactional
  public void insertAccount(Account account) {
    accountMapper.insertAccount(account);
    accountMapper.insertProfile(account);
    accountMapper.insertSignon(account);
  }

  @Transactional
  public void updateAccount(Account account) {
    accountMapper.updateAccount(account);
    accountMapper.updateProfile(account);

    if (account.getPassword() != null && account.getPassword().length() > 0) {
      accountMapper.updateSignon(account);
    }
  }

  public void saveAccount(Account account){
    accountMapper.insert(account);
  }

}
